﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PIC.Commands;

namespace LEDControllerTools.Controller
{

    public delegate void ToggledEventHandler(object sender, bool isOn);
    
    public class DigitalOutChannel : IToggleable
    {
        public event ToggledEventHandler Toggled;

        private readonly IExecutable _onCommand;
        private readonly IExecutable _offCommand;


        public bool IsOn { get; private set; }
        public string Id { get; private set; }

        public DigitalOutChannel(string id, IExecutable onCommand, IExecutable offCommand)
        {
            Id = id;
            _onCommand = onCommand;
            _offCommand = offCommand;
        }



        public void On()
        {
            _onCommand.Execute();
            Console.WriteLine(string.Format(@"{0} is On", Id));
            IsOn = true;
            OnToggled(this, true);
        }

        public void Off()
        {
            _offCommand.Execute();
            Console.WriteLine(string.Format(@"{0} is Off", Id));
            IsOn = false;
            OnToggled(this, false);
        }

        public void OnToggled(object sender, bool e)
        {
            if (Toggled != null)
                Toggled(sender, e);
        }
    }
}
